package com.hxgis.oc.cma;

import com.hxgis.oc.cma.util.CmaFileInfo;
import com.hxgis.oc.cma.util.CmaFileRequest;
import com.hxgis.oc.cma.util.CmaUrlGenerator;
import com.hxgis.oc.config.OConfig;
import com.hxgis.oc.product.file.DownloadTask;
import com.hxgis.oc.product.w3d.W3dConvTask;
import com.hxgis.oc.product.base.ProductFactory;
import com.hxgis.oc.product.base.ProductFactoryManager;
import com.hxgis.oc.util.TimerBase;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Collection;
import java.util.List;

@Slf4j
public class CmaTimer3m extends TimerBase {
    public static void start(CmaFileRequest request) {
        TimerBase.start(CmaTimer3m.class.getSimpleName(),new CmaTimer3m(request));
    }

    public CmaTimer3m(CmaFileRequest req){
        super(req.getDataTable(),0,3);
        request = req;
        convVarName = getVarByDataSet(dataSet);
        isUTC = true;
    }

    protected CmaFileRequest request;
    protected String convVarName;

    String getVarByDataSet(String dataSet){
        return SwanRequest.DataCode == dataSet ? "DBZ" : "EF6A0A74";
    }

    protected DownloadTask createATask(long fid,File outputFile){
        DownloadTask task = new DownloadTask(fid, dataSet, outputFile);
        W3dConvTask convTask = new W3dConvTask(fid, dataSet, outputFile, convVarName);
        task.addConvertTask(convTask);
        return task;
    }

    int getIfNoExist(List<CmaFileInfo> fileList){
        int taskCount = 0;
        for(CmaFileInfo cmaFileInfo : fileList)
        {
            long fid = cmaFileInfo.getTimeStamp();
            if(fileProductManager.exist(dataSet,fid)){
                continue;
            }

            File file = fileProductManager.getFile(dataSet,cmaFileInfo.getFileName());

            DownloadTask task = createATask(fid,file);

            if(file.exists()){
                if(file.length() == cmaFileInfo.getFileSize()) {
                    ProductFactoryManager.publish(task);
                    continue;
                } else {
                    file.delete();
                }
            }

            task.setUrl(CmaUrlGenerator.generateFileUrl(cmaFileInfo.getFurtive()));
            task.setChecker(t->{
                DownloadTask downloadTask = (DownloadTask)(t);
                return downloadTask.getOutputFile().length() == cmaFileInfo.getFileSize();
            });
            fileProductManager.getWorker().addTask(task);
            taskCount++;
        }

        return taskCount;
    }

    @Override
    public void run() {
        updateTime();

        if(!isTimeToUpdate()){
            return;
        }

        List<CmaFileInfo> fileList = request.query(timeToday00,timeCurrent);

        if(fileList.isEmpty()){
            return;
        }

        int count1 = fileList.size();
        int count2 = fileProductManager.count(dataSet, timeToday00);

        if(count1 == count2) {
            return;
        }

        StringBuilder stringBuilder = new StringBuilder();
        int taskCount = getIfNoExist(fileList);
        stringBuilder.append(String.format(" 远程[%d]",count1));
        stringBuilder.append(String.format(" 本地[%d]",count2));
        stringBuilder.append(String.format(" 队列[%d]",taskCount));
        Collection<ProductFactory> factoryList = ProductFactoryManager.getFactoryCollection();
        for(ProductFactory factory : factoryList){
            if(fileProductManager.getClass() != factory.getClass() && factory.accept(dataSet)){
                String productType = factory.getProductType();
                int count = factory.count(dataSet);
                stringBuilder.append(String.format(" %s:[%d]",productType,count));
            }
        }
        log.info("[{} {}至现在] :{}", dataSet,OConfig.formatLogString(timeToday00,isUTC),stringBuilder);
    }
}
